package net.wds.study.major.controller;/**
 * Package: net.wds.study.major.controller
 * Description：
 * Author: changxiaohu
 * Date: Created in 2020/9/16 11:53
 * Company:
 * Version: 0.0.1
 */

import net.sf.json.JSONObject;
import net.wds.study.major.entity.Major;
import net.wds.study.major.service.MajorService;
import net.wds.study.student.modal.PageModal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import sun.misc.Request;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * @ClassName : MajorController  
 * @Description : 控制对专业的增删查改  
 * @Author : changxiaohu  
 * @Date: 2020-09-16 11:53  
 */
@RequestMapping(value = "/major")
@Controller
public class MajorController {
    @Autowired
    private MajorService majorService;
    @RequestMapping(value = "/getPage",method = {RequestMethod.POST})
    public void getPageQuery(@RequestParam Integer startIndex,
                             @RequestParam Integer pageSize,
                             @RequestParam String name,
                             HttpServletResponse response){
        PageModal<Major> pageQuery = majorService.getPageQuery(startIndex, pageSize, name);
        try{
            response.getWriter().write(JSONObject.fromObject(pageQuery).toString());
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    @RequestMapping(value = "/addMajor")
    public void addMajor(Major major,HttpServletResponse response){
        try{
            Serializable serializable = majorService.addMajor(major);
            response.getWriter().write("{ \"success\":true,\"id\":"+serializable+",\"message\":\"保存成功\"}");
        }catch(Exception e){
            e.printStackTrace();
            try{
                response.getWriter().write("{ \"success\":false,\"id\":null,\"message\":\"保存失败\"}");
            }catch(IOException e1){
                e1.printStackTrace();
            }
        }
    }

    @RequestMapping(value = "/updateMajor",method = {RequestMethod.POST})
    public void updateMajor(Major major,HttpServletResponse response){
        try{
            majorService.updateMajor(major);
            response.getWriter().write("{ \"success\":true,\"id\":null,\"message\":\"修改成功\"}");
        }catch(Exception e){
            e.printStackTrace();
            try{
                response.getWriter().write("{ \"success\":false,\"id\"null,\"message\"修改失败\"}");
            }catch(IOException e1){
                e1.printStackTrace();
            }
        }
    }
    @RequestMapping(value = "/deleteMajor",method = {RequestMethod.POST})
    public void deleteMajor(@RequestParam Integer id,HttpServletResponse response){
        try{
            majorService.deleteMajor(id);
            response.getWriter().write("{\"success\":true,\"id\":null,\"message\":\"删除成功\"}");
        }catch(Exception e){
            e.printStackTrace();
            try{
                response.getWriter().write("{ \"success\":false,\"id\":null,\"message\":\"删除失败\"}");
            }catch (IOException e1){
                e1.printStackTrace();
            }
        }
    }
}
